package pl.edu.pw.elka.dzielins.revanalyzer.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import pl.edu.pw.elka.dzielins.revanalyzer.instance.InstanceObject;

import edu.mit.csail.sdg.alloy4compiler.ast.Expr;
import edu.mit.csail.sdg.alloy4compiler.ast.Sig.Field;

public class ModelRelation {
	private String name;
	private List<ModelObject> arguments;
	private Field field;
	private ModelObject owner;
	
	public ModelRelation(ModelObject owner, String name, Field field, ModelObject... arguments) {
		if (StringUtils.isBlank(name))
			throw new IllegalArgumentException("\"" + name
					+ "\" is not valid relation name");
		if (arguments == null || arguments.length < 1)
			throw new IllegalArgumentException(
					"Relation has to have at least one argument");
		this.name = name;
		this.field = field;
		this.owner = owner;
		this.arguments = new LinkedList<ModelObject>();
		for (int i = 0; i < arguments.length; ++i)
			this.arguments.add(arguments[i]);
	}
	
	// getters & setters
	
	public String getName() {
		return name;
	}

	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

	public void setName(String name) {
		if (StringUtils.isBlank(name))
			throw new IllegalArgumentException("\"" + name
					+ "\" is not valid relation name");
		this.name = name;
	}

	public List<ModelObject> getArguments() {
		return arguments;
	}

	public void setArguments(List<ModelObject> arguments) {
		if (arguments == null || arguments.size() < 1)
			throw new IllegalArgumentException(
					"Relation has to have at least one argument");
		this.arguments = arguments;
	}
	
	public ModelObject getOwner() {
		return owner;
	}

	public void setOwner(ModelObject owner) {
		this.owner = owner;
	}
	
	// other methods
	
	public List<ModelObject> getArgumentsWithOwner() {
		List<ModelObject> result = new ArrayList<ModelObject>(1+arguments.size());
		result.add(owner);
		result.addAll(arguments);
		return result;
	}

	@Override
	public String toString() {
		String s = getName()+" : ";
		for (int i=0;i<arguments.size()-1;++i)
			s+=arguments.get(i).getName()+" -> ";
		s+=arguments.get(arguments.size()-1).getName();
		return s;
	}
}
